<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //存储副作用的桶
        const bucket = new Set();
        //原始数据
        const data = {
            text: "hello word"
        }

        let activeEffect;


        const obj = new Proxy(data, {
            get(target, key) {
                if (activeEffect) {
                    bucket.add(activeEffect);
                }
                //返回值
                return target[key];
            },
            set(target, key, newVal) {
                target[key] = newVal;
                bucket.forEach(fn => fn());
                return true;
            }
        })

        //用于注册副作用函数
        function effect(fn) {
            activeEffect = fn;
            fn();
        }
        effect(() => {
            //会被text2赋值，触发运行
            console.log("更新")
                //这里需要用到的是proxy对象下的text了，而不是data.text
            document.body.innerText = obj.text;
        });

        setTimeout(() => {
            //这里需要用到的是proxy对象下的text了，而不是data.text
            obj.text = "hello world???????"
        }, 1000)
        setTimeout(() => {
            //这里需要用到的是proxy对象下的text了，而不是data.text
            obj.text2 = "hello world???????"
        }, 1000)
    </script>

</body>

</html>